//封装简单的弹窗
function admin_alert(title, icon) {
    Swal.fire({
        icon: icon,
        title: title,
        allowOutsideClick: false,
    }).then($(".swal2-actions button").click(function () {
        history.go();
    }))

}

//添加一个博客类别
function addBlogType() {
    Swal.fire({
        title: '添加的类别',
        input: 'text',
        inputAttributes: {
            autocapitalize: 'off'
        },
        showCancelButton: true,
        confirmButtonText: 'Submit',
    }).then((result) => {
        if (result.isConfirmed) {
            let value = $(".swal2-input").val();
            $.ajax({
                url: "/admin/addBlogTypeByName/",
                type: "post",
                dataType: "json",
                data: {"typeName": value},
                success: function (data) {
                    if (data === true) {
                        admin_alert("添加成功🥰", 'success')
                    } else {
                        admin_alert("添加失败😱", 'error')
                    }
                },
                error: function () {
                    admin_alert("重复添加😱", 'error')
                }
            })
        }
    })
}

//修改一个博客类别
function updateBlogType(typeName, id) {
    Swal.fire({
        title: '修改类别',
        input: 'text',
        inputValue: typeName,
        inputAttributes: {
            autocapitalize: 'off'

        },
        showCancelButton: true,
        confirmButtonText: 'Submit',
    }).then((result) => {
        if (result.isConfirmed) {
            let value = $(".swal2-input").val();
            $.ajax({
                url: "/admin/alterBlogTypeById/",
                type: "post",
                dataType: "json",
                data: {
                    "typeName": value,
                    "id": id
                },
                success: function (data) {
                    if (data === true) {
                        admin_alert("修改成功🥰", 'success')
                    } else {
                        admin_alert("修改失败😱", 'error')
                    }
                },
                error: function () {
                    admin_alert("修改失败😱", 'error')
                }
            })
        }
    })
}

function deleteBlogType(data) {
    Swal.fire({
        title: '确认删除这个类别😱?',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes'
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: "/admin/deleteBlogTypeById/",
                type: "delete",
                dataType: "json",
                data: {"id": data},
                success: function (data) {
                    if (data === true) {
                        admin_alert("删除成功🥰", 'success')
                    } else {
                        admin_alert("删除失败😱", 'error')
                    }
                },
                error: function () {
                    admin_alert("删除失败😱", 'error')
                }
            })
        }
    })
}


//不想做的功能，就弹出一个弹窗解释一下
function Sorry() {
    Swal.fire({
        icon: 'error',
        title: '老哥，所有的信息都在左边！自己看！😀',
        allowOutsideClick: false,
    })
}

function delete_Type() {
    Swal.fire({
        icon: 'error',
        title: '对，这个功能也没有，给我一个一个删😒',
        allowOutsideClick: false,
    })
}